package com.example.hotel_lxy.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.example.hotel_lxy.R;
import com.example.hotel_lxy.application.ARouterPath;
import com.example.hotel_lxy.bean.AnniversaryBean;
import com.example.hotel_lxy.bean.BeanBean;
import com.example.hotel_lxy.bean.CustomerDetailsHistoryBean;
import com.example.hotel_lxy.net.Api;
import com.example.hotel_lxy.ui.dialog.AddAnniversaryDialog;
import com.example.hotel_lxy.utils.SPUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.ysxsoft.common_base.adapter.BaseQuickAdapter;
import com.ysxsoft.common_base.adapter.BaseViewHolder;
import com.ysxsoft.common_base.base.BaseActivity;
import com.ysxsoft.common_base.base.RBaseAdapter;
import com.ysxsoft.common_base.base.RViewHolder;
import com.ysxsoft.common_base.base.frame.list.IListAdapter;
import com.ysxsoft.common_base.base.frame.list.ListManager;
import com.ysxsoft.common_base.utils.JsonUtils;
import com.ysxsoft.common_base.view.widgets.MultipleStatusView;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Call;

/**
 * 纪念日
 */
@Route(path = "/main/AnniversaryActivity")
public class AnniversaryActivity extends BaseActivity implements View.OnClickListener , IListAdapter<AnniversaryBean.DataBean.ListBean> {


    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.text_add_anniversary)
    TextView textAddAnniversary;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.multipleStatusView)
    MultipleStatusView multipleStatusView;
    @BindView(R.id.smartRefresh)
    SmartRefreshLayout smartRefresh;
    private List<String> mlist = new ArrayList<>();
    ListManager<AnniversaryBean.DataBean.ListBean> manager;
    @Autowired
    String id;

    public static void start(String id) {
        ARouter.getInstance().build(ARouterPath.getAnniversaryActivity())
                .withString("id", id)
                .navigation();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_anniversary;
    }

    @Override
    public void doWork() {
        super.doWork();
        ARouter.getInstance().inject(this);
        back.setOnClickListener(this);
        textAddAnniversary.setOnClickListener(this);
        showView();
    }

    private void showView() {
        manager = new ListManager(this);
        manager.init(getWindow().findViewById(android.R.id.content));
        manager.getAdapter().setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

            }
        });
        manager.getAdapter().setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                request(manager.nextPage());
            }
        }, recyclerView);
        request(1);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.text_add_anniversary:
                AddAnniversaryDialog.show(this, null,id).setListener(new AddAnniversaryDialog.OnDialogClickListener() {
                    @Override
                    public void onPositiveClick(String s) {
                        if(s!=null){
                            request(1);
                        }
                    }
                });
                break;
        }
    }

    @Override
    public int getItemLayoutId() {
        return R.layout.item_anniversary_activity;
    }

    @Override
    public void request(int page) {
        OkHttpUtils.post()
                .url(Api.POST_REMENMBER)
                .addHeader("flag", SPUtils.get(AnniversaryActivity.this, "token", "").toString())
                .addParams("id", id + "")
                .addParams("page", page + "")
                .tag(this)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Log.e("TAG", "查看纪念日onError   " + e.getMessage());
                        manager.releaseRefresh();
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        Log.e("TAG", "查看纪念日onResponse   " + response);
                        manager.releaseRefresh();
                        BeanBean beanBean= JsonUtils.parseByGson(response, BeanBean.class);
                        if(beanBean.getCode().equals("4")){
                            SPUtils.put(AnniversaryActivity.this,"token","");
                            Intent intent = new Intent(AnniversaryActivity.this, LoginActivity.class).
                                    setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                            startActivity(intent);
                            return;
                        }
                        AnniversaryBean anniversaryBean = JsonUtils.parseByGson(response, AnniversaryBean.class);
                        if(beanBean.getCode().equals("0")){
                            if(page==1){
                                manager.resetPage();
                            }
                            manager.setData(anniversaryBean.getData().getList());
                        }
                    }
                });
    }

    @Override
    public void fillView(BaseViewHolder helper, AnniversaryBean.DataBean.ListBean s) {
        TextView text_user_name=helper.itemView.findViewById(R.id.text_user_name);
        text_user_name.setText(s.getRelation()+"      "+s.getRemember());
        TextView text_time=helper.itemView.findViewById(R.id.text_time);
        text_time.setText(""+s.getDay());
        ImageView img_delete=helper.itemView.findViewById(R.id.img_delete);
        img_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showDelete(s.getId());
            }
        });
    }

    private void showDelete(int id) {
        OkHttpUtils.post()
                .url(Api.POST_DELETE)
                .addHeader("flag", SPUtils.get(AnniversaryActivity.this, "token", "").toString())
                .addParams("id", id + "")
                .tag(this)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Log.e("TAG", "删除纪念日onError   " + e.getMessage());
                        manager.releaseRefresh();
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        Log.e("TAG", "删除纪念日onResponse   " + response);
                        BeanBean beanBean = JsonUtils.parseByGson(response, BeanBean.class);
                        if(beanBean.getCode().equals("0")){
                           request(1);
                        }else if(beanBean.getCode().equals("4")){
                            SPUtils.put(mContext,"token","");
                            Intent intent = new Intent(mContext, LoginActivity.class).
                                    setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                            mContext.startActivity(intent);
                        }

                    }
                });
    }

    @Override
    public void fillMuteView(BaseViewHolder helper, AnniversaryBean.DataBean.ListBean s) {

    }

    @Override
    public void attachActivity(AppCompatActivity activity) {

    }

    @Override
    public void dettachActivity() {

    }

    @Override
    public RecyclerView.LayoutManager getLayoutManager() {
        return new LinearLayoutManager(this);
    }

    @Override
    public boolean isMuteAdapter() {
        return false;
    }

    @Override
    public int[] getMuteTypes() {
        return new int[0];
    }

    @Override
    public int[] getMuteLayouts() {
        return new int[0];
    }
}